//
//  PBDeputeViewController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/11/21.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBDeputeViewController.h"
#import "JCAlertView.h"
#import "PBOnlineDeputeController.h"

@interface PBDeputeViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *telDeputeButton;
@property (weak, nonatomic) IBOutlet UIButton *onlineDeputeButton;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) JCAlertView *alert;
@end

@implementation PBDeputeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}
- (void)configureView {
    self.webView.delegate = self;
    self.telDeputeButton.layer.cornerRadius = 5;
    self.telDeputeButton.layer.borderColor = UIColorWithRGB(233, 166, 41).CGColor;
    self.telDeputeButton.layer.borderWidth = 0.5f;
    self.telDeputeButton.layer.masksToBounds = YES;
    self.onlineDeputeButton.layer.cornerRadius = 5;
    self.onlineDeputeButton.layer.masksToBounds = YES;
}
- (void)configureData {
    [TMNetwork requestGetWithParam:@{} url:@"/sys/config" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            for (NSDictionary *dic in model.data) {
                if ([dic[@"label"] isEqualToString:@"entrust_h5"]) {
                    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:dic[@"value"]]];
                    [self.webView loadRequest:request];
                }
            }
        }else {
            [self showHint:model.message];
        }
    }];
}

// 电话咨询
- (IBAction)telButtonClick:(id)sender {
    // 弹出框背景
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 130)];
    customView.backgroundColor = [UIColor whiteColor];
    
    // 弹出框标题
    UILabel *titlelabel = [[UILabel alloc] init];
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.font = [UIFont boldSystemFontOfSize:14];
    titlelabel.text = @"房屋委托";
    [customView addSubview:titlelabel];
    [titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(26);
    }];
    
    // 温馨提示
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.textAlignment = NSTextAlignmentLeft;
    tipLabel.font = [UIFont systemFontOfSize:13];
    tipLabel.text = @"拨打电话进行委托：400008000";
    tipLabel.textColor = [UIColor tm_majorFontColor];
    [customView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(50);
    }];
    
    // 横线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor tm_separationBgColor];
    [customView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(90);
        make.height.mas_equalTo(1);
    }];
    // 取消
    UIButton *cancelButton = [[UIButton alloc] init];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [cancelButton setTitleColor:[UIColor tm_majorFontColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo((kScreenWidth - 80)/2);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo (91);
        make.left.mas_equalTo (0);
    }];
    
    // 确定
    UIButton *sureButton = [[UIButton alloc] init];
    [sureButton setTitle:@"现在拨打" forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [sureButton setTitleColor:[UIColor tm_majorFontColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo((kScreenWidth - 80)/2);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo (91);
        make.right.mas_equalTo (0);
    }];
    
    // 竖线
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor tm_separationBgColor];
    [customView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((kScreenWidth - 80)/2);
        make.top.mas_equalTo(101);
        make.width.mas_equalTo(1);
        make.bottom.mas_equalTo(-10);
    }];
    
    self.alert = [[JCAlertView alloc] initWithCustomView:customView dismissWhenTouchedBackground:NO];
    self.alert.layer.cornerRadius = 5;
    self.alert.layer.masksToBounds = YES;
    [self.alert show];
}

- (void)cancelButtonClick {
    [self.alert dismiss];
}
- (void)sureButtonClick {
    [self.alert dismiss];
    NSString *str = @"tel://40008000";
    UIWebView *callWebView = [[UIWebView alloc] init];
    [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebView];
}


#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (![request.URL isEqual:[NSURL URLWithString:@"http://rzccq.com/h5/html/entrust/"]]) {
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backButton"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemClick:)];
        self.navigationItem.leftBarButtonItem = backItem;
    }else {
        self.navigationItem.leftBarButtonItem = nil;
    }
    return YES;
}


// 在线委托
- (IBAction)onlineButtonClick:(id)sender {
    PBOnlineDeputeController *vc = [[PBOnlineDeputeController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - super
- (void)resetNavigationBarItems {
    self.title = @"房屋委托";
}

- (void)backBarButtonItemClick:(UIBarButtonItem *)sender {
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        [self.view resignFirstResponder];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
